﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ASPNET.StarterKit.BusinessLogicLayer;
using ASPNET.StarterKit.DataAccessLayer;
using System.Data.SqlClient;


public partial class InventoryTracker_Materials : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnMatsNew.Enabled = true;
        btnMatsDelete.Enabled = false;
        btnMatsEdit.Enabled = false;
        btnSubmit.Enabled = true;
        btnMatsUpdate.Enabled = false;

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DataAccess DA = DataAccessHelper.GetDataAccess();
        Materials aMaterials =new Materials(int.Parse( ddrMaterialsItemCode.SelectedValue),txtMaterialName.Text,float.Parse(txtMaterialDisc.Text));
        try
        {
            if (DA.CreateMaterials(aMaterials))
            {
                lblResult.Text = "Success";
            }
            else
            {
                lblResult.Text = "Failure";
            }
        }
        catch (Exception ex)
        {
            lblResult.Text = ex.Message.ToString();
        }
    }
    protected void DdlMaterialName_SelectedIndexChanged(object sender, EventArgs e)
    { 
        String SelectValue = DdlMaterialName.SelectedValue;
        ddrMaterialsItemCode.Visible= false;
        txtVisItemName.Visible = true;

       btnMatsNew.Enabled= true;
        btnMatsDelete.Enabled = true;
        btnMatsEdit.Enabled= true;
        btnSubmit.Enabled = false;
        btnMatsUpdate.Enabled = false;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "SELECT * FROM  Materials WHERE  [MaterialID]=" + SelectValue;
        
        SqlCommand cmd = new SqlCommand(sql, con);
        DataTable dataTable = GetDataTable(cmd);
        txtMaterialName.Text = dataTable.Rows[0]["MaterialName"].ToString();
        txtMaterialDisc.Text = dataTable.Rows[0]["MaterialDisc"].ToString();
        

        String sqlItemCode = "SELECT * FROM MaterialMaster WHERE ItemCode=" + dataTable.Rows[0]["ItemCode"];
        SqlCommand cmdItemCode = new SqlCommand(sqlItemCode, con);
        DataTable dataTableItemCode = GetDataTable(cmdItemCode);
        txtVisItemName.Text = dataTableItemCode.Rows[0]["ItemName"].ToString();
        
        
        con.Close();
 
     }

    public static DataTable GetDataTable(SqlCommand cmd)
    {

        SqlDataAdapter da = new SqlDataAdapter();
        //set the SelectCommand of the adapter
        da.SelectCommand = cmd;
        // create a new DataTable
        DataTable dtGet = new DataTable();
        //fill the DataTable
        da.Fill(dtGet);
        //return the DataTable
        return dtGet;

    }

    protected void btnMatsNew_Click(object sender, EventArgs e)
    {
        ddrMaterialsItemCode.Visible= true;
        txtVisItemName.Visible = false;

        btnMatsNew.Enabled = false;
        btnMatsDelete.Enabled = false;
        btnMatsEdit.Enabled = false;
        btnSubmit.Enabled =true;
        btnMatsUpdate.Enabled = false;


        txtMaterialName.Text = "";
        txtMaterialDisc.Text = "";
        ddrMaterialsItemCode.SelectedIndex = 0;
    }
    protected void btnMatsDelete_Click(object sender, EventArgs e)
    {
        String SelectValue = DdlMaterialName.SelectedValue;

        btnMatsNew.Enabled = false;
        btnMatsDelete.Enabled = true;
        btnMatsEdit.Enabled = false;
        btnSubmit.Enabled = false;
        btnMatsUpdate.Enabled =false;


        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "DELETE FROM Materials WHERE  [MaterialID]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

        txtMaterialName.Text = "";
        txtMaterialDisc.Text = "";
        ddrMaterialsItemCode.SelectedIndex = 0;
    
    }
    protected void btnMatsEdit_Click(object sender, EventArgs e)
    {
        ddrMaterialsItemCode.Visible = true;
        txtVisItemName.Visible= false;

        btnMatsNew.Enabled = false;
        btnMatsDelete.Enabled = false;
        btnMatsEdit.Enabled = false;
        btnSubmit.Enabled = false;
        btnMatsUpdate.Enabled = true;


        
    }
    protected void btnMatsUpdate_Click(object sender, EventArgs e)
    {
        btnMatsNew.Enabled = false;
        btnMatsDelete.Enabled = false;
        btnMatsEdit.Enabled = false;
        btnSubmit.Enabled = false;
        btnMatsUpdate.Enabled = true;



        String SelectValue = DdlMaterialName.SelectedValue;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "UPDATE Materials SET  MaterialName = '" + txtMaterialName.Text + "' , MaterialDisc = '" + txtMaterialDisc.Text + "'  WHERE MaterialID =" + SelectValue;
        //String sql= "UPDATE LocationMaster SET LocationName = @LocationName, LocAddr1 = @LocAddr1, LocAddr2 = @LocAddr2, LocCity = @LocCity, LocState = @LocState, LocCountry = @LocCountry, LocPhone = @LocPhone, LocFax = @LocFax, Remarks = @Remarks WHERE [LocationID] = " + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();


        txtMaterialName.Text = "";
        txtMaterialDisc.Text = "";
        ddrMaterialsItemCode.SelectedIndex = 0;
    }
    }


